package feevale.dropbox.main;

import java.io.File;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import feevale.dropbox.util.Connection;
import feevale.dropbox.util.DeletedFileMonitor;

public class Server {
	
	/**
	 * Porta de conexão ao servidor
	 */
	private static final Integer PORT = 8899;
	
	/**
	 * List de {@link Connection} que representa todas as conexões clientes ao servidor
	 */
	public static List<Connection> CONNECTIONS = new ArrayList<Connection>();
	
	/**
	 * Arquivos contidos no servidor
	 */
	public static List<File> EXISTENT_FILES = new ArrayList<File>();
	
	public static List<File> DELETED_FILES = new ArrayList<File>();
	
	public static final String FOLDER = "C:\\Temp\\server";
	
	public static Map<String, Connection> MAP = new HashMap<String, Connection>();
	
	public static void main(String[] args) {
		Server server = new Server();
		
		setup();
		
		server.setupConnections();
	}
	
	private static void setup() {
		for (File file : getFolder().listFiles()) {
			if (file.isFile()) {
				EXISTENT_FILES.add(file);
			}
		}
	}

	private void setupConnections() {
		try {
			ServerSocket serverSocket = new ServerSocket(PORT);
			CONNECTIONS = new ArrayList<Connection>();
			
			while (true) {
				Connection connection = new Connection(serverSocket.accept());
				CONNECTIONS.add(connection);
				connection.setup();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static File getFolder() {
		return new File(FOLDER);
	}
	
}
